package com.example.adms.leave.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.adms.leave.dto.*;
import com.example.adms.leave.entity.Leave;
import com.example.adms.leave.mapper.LeaveMapper;
import com.example.adms.leave.service.LeaveService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hyt
 * @since 2023-04-20
 */
@Service
public class LeaveServiceImpl extends ServiceImpl<LeaveMapper, Leave> implements LeaveService {
    @Autowired
    LeaveMapper leaveMapper;

    @Override
    public int leaveTime(String userId) {
        DayInterval dayInterval = new DayInterval();

        QueryWrapper<Leave> queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",userId)
                .ge("submit_time",dayInterval.getFirstDay())
                .le("submit_time",dayInterval.getSecondDay());
        int result = this.count(queryWrapper);
        return result;
    }

    @Override
    public int addLeave(leaveMsg msg) {
        if (ObjectUtils.isNull(msg.getStartTime()) || ObjectUtils.isNull(msg.getEndTime())){
            return 3;
        }
        msg.setComments(null);
        msg.setPass(0);
        int leavetime = leaveTime(msg.getUserId());
        int sysLeaveTime = leaveMapper.selectSysLeaveTime();
        if(leavetime < sysLeaveTime && leavetime >=0){
            leaveMapper.insertLeave(msg);
            return 1;
        }else if (leavetime>=5){
            return 2;
        }else {
            return 3;
        }
    }

    @Override
    public Map<String, Object> getLeaveList(QueryLeave queryLeave) {
        Page<Leave> page = new Page<>(queryLeave.getPageNumber(),queryLeave.getPageSize());
        LambdaQueryWrapper<Leave> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(queryLeave.getUserId() != null,Leave::getUserId,queryLeave.getUserId());
        queryWrapper.ge(queryLeave.getStartTime()!=null,Leave::getStartTime,queryLeave.getStartTime())
                .le(queryLeave.getEndTime()!=null,Leave::getEndTime,queryLeave.getEndTime());
        queryWrapper.eq(queryLeave.getPass()!=null,Leave::getPass,queryLeave.getPass());
        Page<Leave> leavePage = this.page(page,queryWrapper);
        List<Leave> leaves = leavePage.getRecords();
        Long total = leavePage.getTotal();
        Map<String,Object> map = new HashMap<>();
        map.put("list",leaves);
        map.put("total",total);
        return map;
    }

    @Override
    public Map<String, Object> LeaderGetLeaveList(QueryLeave queryLeave) {

        Page<Leave> page = new Page<>(queryLeave.getPageNumber(), queryLeave.getPageSize());
        Page<Leave> leavePage = leaveMapper.leaderSelectRecorde(page,queryLeave);
        Long total = leavePage.getTotal();
        List<Leave> leaves = leavePage.getRecords();
        Map<String,Object> map = new HashMap<>();
        map.put("list",leaves);
        map.put("total",total);
        return map;
    }

    @Override
    public Map<String, Object> employeeLeaveList(QueryLeave queryLeave) {
        //员工
        Page<Leave> page = new Page<>(queryLeave.getPageNumber(),queryLeave.getPageSize());
        LambdaQueryWrapper<Leave> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(true,Leave::getUserId,queryLeave.getUserId());
        queryWrapper.ge(queryLeave.getStartTime()!=null,Leave::getStartTime,queryLeave.getStartTime())
                .le(queryLeave.getEndTime()!=null,Leave::getEndTime,queryLeave.getEndTime());
        queryWrapper.eq((Integer)queryLeave.getPass()!=null,Leave::getPass,queryLeave.getPass());
        Page<Leave> leavePage = this.page(page,queryWrapper);
        List<Leave> leaves = leavePage.getRecords();
        Long total = leavePage.getTotal();
        Map<String,Object> map = new HashMap<>();
        map.put("list",leaves);
        map.put("total",total);
        return map;
    }

    @Override
    public List<Object> leaveReport(leaveReport report) {
        //获取所有部门id
        List<leaveDepart> depart = leaveMapper.selectLeaveDepart();
        List<Object> resultList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        String startTime = new DayInterval().getFirstDayOfMonth(year,report.getMonth());
        String endTime = new DayInterval().getLastDayOfMonth(year, report.getMonth());
        for (leaveDepart leaveDepart : depart){
            Map<String,Object> map = new HashMap<>();
            Map<String,Object> param = new HashMap<>();
            param.put("startTime", startTime);
            param.put("endTime", endTime);
            param.put("departId", leaveDepart.getDepartId());
            Integer DepartLeaveNum = leaveMapper.selectDepartLeaveNumByDepartId(param);
            map.put("departId",leaveDepart.getDepartId());
            map.put("departName",leaveDepart.getDepartName());
            map.put("departLeaveNum",DepartLeaveNum);
            resultList.add(map);
        }
        return resultList;
    }

    @Override
    public boolean examineLeave(ExamineLeave examineLeave) {
        UpdateWrapper<Leave> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("leave_id",examineLeave.getLeaveId())
                .set("pass", examineLeave.getPass())
                .set("comments",examineLeave.getComments());
        int result = leaveMapper.update(null,updateWrapper);
        if (result != 0){
            examineLeave.setMsg("已审批");
            leaveMapper.addLeaveEvent(examineLeave);
            return true;
        }
        else {
            return false;
        }
    }

    @Override
    public LeaveDetails getLeaveDetails(int leaveId) {
        LeaveDetails leaveDetails = leaveMapper.selectLeaveDetails(leaveId);
        return leaveDetails;
    }

    @Override
    public boolean deleteLeave(int leaveId) {
        QueryWrapper<Leave> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("leave_id",leaveId);
        return this.remove(queryWrapper);
    }


}
